using System;
using System.Xml;
using System.Collections.Generic;
using Gtk;
	
namespace pointmass
{
	public class PointMass
	{
		string id;
		double m_x, 
			   m_y, 
			   m_z;
		double m_vx, 
			   m_vy, 
			   m_vz;
		double m_mass;
		
		public PointMass()
		{
			m_x = 0;
			m_y = 0;
			m_z = 0;
			m_vx = 0;
			m_vy = 0;
			m_vz = 0;
			m_mass = 1;
		}
		
		public void LoadObjectXml(XmlNode objNode)
		{
			XmlAttributeCollection attrs = objNode.Attributes;
			if (attrs["id"] != null) this.id = attrs["id"].InnerText;
			if (attrs["x"] != null) this.m_x = double.Parse (attrs["x"].InnerText);
			if (attrs["y"] != null) this.m_y = double.Parse (attrs["y"].InnerText);
			if (attrs["z"] != null) this.m_z = double.Parse (attrs["z"].InnerText);
			if (attrs["vx"] != null) this.m_vx = double.Parse (attrs["vx"].InnerText);
			if (attrs["vy"] != null) this.m_vy = double.Parse (attrs["vy"].InnerText);
			if (attrs["vz"] != null) this.m_vz = double.Parse (attrs["vz"].InnerText);
			if (attrs["mass"] != null) this.m_mass = double.Parse (attrs["mass"].InnerText);
		}

		public void Draw(Gtk.TextView lbl)
		{
			lbl.Buffer.Text += string.Format ("x={0} y={1} z={2}, vx={3} vy={4} vz={5}, mass={6}, id={7}\n", m_x, m_y, m_z, m_vx, m_vy, m_vz, m_mass, id);
		}
	}
	
	public class MechanicSystem
	{
		public int Count { get { return m_objects.Count; }  }
		List<PointMass> m_objects = new List<PointMass> ();

		public MechanicSystem ()
		{
		}
		
		void AddObjectXml(XmlNode objNode)
		{
			PointMass p = new PointMass ();
			p.LoadObjectXml (objNode);
			m_objects.Add(p);
		}
		
		public void LoadSystemXml(XmlNode systemNode)
		{
			XmlNodeList objectList = systemNode.ChildNodes;
			int n_object = objectList.Count;
		
			for (int i = 0; i < n_object; i++) {
				XmlNode objectNode = objectList.Item (i);
				this.AddObjectXml (objectNode);
			}
		}
		
		public void Draw(Gtk.TextView lbl)
		{
			lbl.Buffer.Text = "";
			for (int i = 0; i < Count; i++) {
				m_objects[i].Draw(lbl);
			}
		}
	}
}

